﻿using System;
using System.Linq;
using System.Reflection;
using ExpressMapper;

namespace Sustainalytics.Entities.Tests
{
    public static class ExpressMapperExtensions
    {
        public static IMemberConfiguration<T, TN> AllDestPresent<T, TN>(this IMemberConfiguration<T, TN> config)
        {
            var sourceProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var destProps = typeof(TN).GetProperties(BindingFlags.Instance | BindingFlags.Public);

            var sourceNames = sourceProps.Select(x => x.Name).ToArray();
            var missingDestProps = destProps.Where(x => !sourceNames.Contains(x.Name)).Select(x => x.Name).ToList();
            if (missingDestProps.Count > 0)
                throw new Exception("Source is missing: " + string.Join(", ", missingDestProps.ToArray()));

            return config;
        }
        public static IMemberConfiguration<T, TN> AllSourcePresent<T, TN>(this IMemberConfiguration<T, TN> config)
        {
            var sourceProps = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var destProps = typeof(TN).GetProperties(BindingFlags.Instance | BindingFlags.Public);

            var destNames = destProps.Select(x => x.Name).ToArray();
            var missingSourceProps = sourceProps.Where(x => !destNames.Contains(x.Name)).Select(x => x.Name).ToList();
            if (missingSourceProps.Count > 0)
                throw new Exception("Source is missing: " + string.Join(", ", missingSourceProps.ToArray()));

            return config;
        }


    }
}
